﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;
using ProjectStorageDB;

/// <summary>
/// Summary description for Utils
/// </summary>
public static class Utils
{
    public static DataTable FromProjectManagerToDataTable(List<ProjectStorageLib.Project.ProjectManager> data,List<string> hidenField = null)
    {
        if (data == null || data.Count() <= 0)
            return null;

        //Create an instance of DataTable
        DataTable dt = new DataTable();

        #region Create column

        //Add project id column
        DataColumn dcol = new DataColumn("ID", typeof(System.String));
        dt.Columns.Add(dcol);

        //Add project name column
        dcol = new DataColumn("Project Name", typeof(System.String));
        dt.Columns.Add(dcol);

        //Add project factors column
        foreach (Factor f in data.First().Factors)
        {
            if (hidenField == null || !hidenField.Contains(f.Name))
            {
                dcol = new DataColumn(f.Name, typeof(System.String));
                dt.Columns.Add(dcol);
            }
        }

        //Add project agile practice
        foreach (AgilePractice ag in data.First().AgilePractices)
        {
            if (hidenField == null || !hidenField.Contains(ag.Name))
            {
                dcol = new DataColumn(ag.Name, typeof(System.String));
                dt.Columns.Add(dcol);
            }
        }

        #endregion

        //Add data

        foreach (ProjectStorageLib.Project.ProjectManager pm in data)
        {
            //Create a new row
            DataRow drow = dt.NewRow();

            //Set project name
            drow["Project Name"] = pm.Project.Name;
            drow["ID"] = pm.Project.ProjectID;

            //add agile practice data
            foreach (ProjectPractice pp in pm.Project.Practices)
            {
                if (hidenField == null || !hidenField.Contains(pp.AgilePractice.Name))
                {
                    switch(pp.value)
                    {
                        case 0:
                            drow[pp.AgilePractice.Name] = "Not applied";
                            break;
                        case 1:
                            drow[pp.AgilePractice.Name] = "Low";
                            break;
                        case 3:
                            drow[pp.AgilePractice.Name] = "Medium";
                            break;
                        case 5:
                            drow[pp.AgilePractice.Name] = "High";
                            break;                        
                    }
                }
            }

            //add factors data
            foreach (ProjectFactorScale fs in pm.Project.FactorValues)
            {
                if (hidenField == null || !hidenField.Contains(fs.Factor.Name))
                {
                    drow[fs.Factor.Name] = fs.FactorScale.Value;
                }
            }

            for (int i = 2; i < drow.ItemArray.Count(); i++)
            {
                if (drow[i] == DBNull.Value || string.IsNullOrEmpty((string)drow[i]))
                        drow[i] = "N/A";
                    
            }

            dt.Rows.Add(drow);
        }

        #region Agregated 

        List<string> values = new List<string> { "High", "Medium", "Low", "Not applied", "N/A" };

        foreach (string str in values)
        {
            //Create a new row
            DataRow drow = dt.NewRow();

            //Set project name
            drow["Project Name"] = str;
            drow["ID"] = "";

            //add agile practice data
            foreach (AgilePractice ap in data.FirstOrDefault().AgilePractices)
            {
                if (hidenField == null || !hidenField.Contains(ap.Name))
                {
                    drow[ap.Name] = (from proj in dt.AsEnumerable()
                                     where proj.Field<string>(ap.Name) == str
                                     select proj).Count();                    
                }
            }            

            dt.Rows.Add(drow);
        }

        #endregion Agregated data

        return dt;
    }    
}